package com.apps.globals.global.service.impl;

import com.apps.globals.global.model.po.KupaGlobal;
import com.apps.globals.global.dao.KupaGlobalMapper;
import com.apps.globals.global.service.IKupaGlobalService;
import com.apps.sannotation.service.OnRedisCache;
import com.apps.sannotation.service.enums.CacheModel;
import com.apps.sworld.dao.impl.sDaoImpl;
import com.apps.globals.global.utils.KupaMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 大鹏
 * @since 2020-10-02
 */
@OnRedisCache(model = CacheModel.ALL)
@Service
public class KupaGlobalServiceImpl extends sDaoImpl<KupaGlobalMapper, KupaGlobal> implements IKupaGlobalService {


    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     *
     * @return
     */
    @Override
    public KupaMap<String, Object> QueryMap()
    {
       return (KupaMap<String, Object>) redisTemplate.opsForValue().get(this.getClass().getSimpleName()+this.currentModelClass().getSimpleName());
    }

    private void QueryAndCacheMasterData()
    {
        List<KupaGlobal> globals=query();
        KupaMap<String, Object> result=new KupaMap<>();
        for(KupaGlobal global:globals)
            result.put(global.getProd_key(),global.getKey_value());
        redisTemplate.opsForValue().set(this.getClass().getSimpleName()+this.currentModelClass().getSimpleName(),result);
    }

    @Override
    public void onLoad()
    {
        //将所有一级分类加载到缓存
        QueryAndCacheMasterData();
    }

    @Override
    public void onCreate(KupaGlobal global)
    {
        //将所有一级分类加载到缓存
        QueryAndCacheMasterData();
    }

    @Override
    public void onUpdate(KupaGlobal global)
    {
        //将所有一级分类加载到缓存
        QueryAndCacheMasterData();
    }

    @Override
    public void onDelete(List<KupaGlobal> globals)
    {
        //将所有一级分类加载到缓存
        QueryAndCacheMasterData();
    }
}
